package com.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dto.OrderDto;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Async;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

@Slf4j
public class OrderService {


    @Async
    public void batchImport(List<OrderDto> orderLIst) throws InterruptedException {
        log.info("批量导入订单");
        Thread.sleep(500);
    }


    @Async
    public CompletableFuture<List<OrderDto>> asyncOrderList() throws InterruptedException {

        TimeUnit.SECONDS.sleep(1);

        final List<OrderDto> result = Arrays.asList(new OrderDto("电话"), new OrderDto("大卡车"));

        return CompletableFuture.completedFuture(result);
    }

    @Cacheable(cacheNames = "basicOrderList")
    public List<OrderDto> orderList(Page<OrderDto> page) {
        return exampleOrderList();
    }

    @NotNull
    private ArrayList<OrderDto> exampleOrderList() {
        final ArrayList<OrderDto> result = new ArrayList<>();
        result.add(new OrderDto("电话"));
        result.add(new OrderDto("大卡车"));
        return result;
    }

    @Cacheable(cacheNames = "hello_",key = "#root.methodName+'_'+#root.args[0].current+'_'+#root.args[0].size")
    public List<OrderDto> closedOrderList(Page<OrderDto> page){

        return exampleOrderList();

    }

    @Cacheable(cacheNames = "paying_",keyGenerator = "argsFieldGenerator")
    public List<OrderDto> payingOrderList(Page<OrderDto> page){

        return exampleOrderList();

    }

    @Cacheable(cacheNames = "paying_",sync = true,condition = "#root.args[0].current == 1")
    public List<OrderDto> payedOrderList(Page<OrderDto> page){

        return exampleOrderList();

    }
}
